package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.mapper.system.ISysLogMapper;
import com.wejoy.video.middle.station.data.domain.system.SysLog;
import com.wejoy.video.middle.station.data.service.system.ISysLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 日志表
 *
 * @author admin
 * @date 2022年09月16日 12:41:35
 */
@Service
public class SysLogServiceImpl implements ISysLogService {
    @Autowired
    private ISysLogMapper sysLogMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(SysLog req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysLog req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysLogMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysLog sysLog) {
        WJAssert.notNull(sysLog, "入参对象不能为空");
        WJAssert.notNull(sysLog.getId(), "id参数错误");
        SysLog where = new SysLog();
        where.setId(sysLog.getId());
        return sysLogMapper.updateSelective(sysLog, where);
    }

    @Override
    public int getCount(SysLog sysLog) {
        checkStringLength(sysLog, true);
        return sysLogMapper.getCount(sysLog);
    }

    @Override
    public SysLog getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysLog where = new SysLog();
        where.setId(id);
        return sysLogMapper.getOne(where);
    }

    @Override
    public List<SysLog> getList(SysLog sysLog) {
        checkStringLength(sysLog, true);
        return sysLogMapper.getList(sysLog);
    }

    @Override
    public List<SysLog> getPageList(SysLog sysLog) {
        checkStringLength(sysLog, true);
        return sysLogMapper.getPageList(sysLog);
    }

    @Override
    public void audit(Long id, String operator) {
        SysLog req = new SysLog();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysLog req = new SysLog();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysLog req = new SysLog();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysLog sysLog, boolean nullAble) {
        WJAssert.notNull(sysLog, "入参对象不能为空");
        WJAssert.limitMaxLength(sysLog.getTitle(), true, 500, "日志标题不合法!");
        WJAssert.limitMaxLength(sysLog.getRemoteAddr(), true, 255, "操作IP地址不合法!");
        WJAssert.limitMaxLength(sysLog.getUserAgent(), true, 255, "用户代理不合法!");
        WJAssert.limitMaxLength(sysLog.getRequestUri(), true, 255, "请求URI不合法!");
        WJAssert.limitMaxLength(sysLog.getMethod(), true, 10, "操作方式不合法!");
        WJAssert.limitMaxLength(sysLog.getParams(), true, 1000, "操作提交的数据不合法!");
        WJAssert.limitMaxLength(sysLog.getException(), true, 1000, "异常信息不合法!");
        WJAssert.limitMaxLength(sysLog.getOperator(), nullAble, 50, "操作人不合法!");
        WJAssert.limitMaxLength(sysLog.getRemark(), true, 255, "备注不合法!");
    }
}
